#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#  Copyright (c) JoinQuant Development Team
#
#  Author: Huayong Kuang <kuanghuayong@joinquant.com>
#  CreateTime: 2017-10-23 16:27:43 Monday
# *************************************************************

import logging as log
from time import time

class Timed():
    """A simple "timer" context manager. It prints execution time."""

    def __init__(self):
        print "-" * 50, "in init"

    def __enter__(self):
        print "-" * 50, "in enter"
        self.start = time()
        print("Starting at {}".format(self.start))
        return self

    def __exit__(self, type, value, traceback):
        # This code is guaranteed to run
        if traceback:
            log.exception(value)

        self.end = time()
        total = self.end - self.start
        print("Ending at {} (total: {})".format(self.end, total))

# Script starts from here

if __name__ == "__main__":
    with Timed() as tm:
        raise Exception("hello world")
